package com.quick.proxy.client.handler.real;


import com.quick.proxy.client.ProxyResources;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * Description: 真实代理连接处理程序
 * Author: 吃素不吃肉
 * Date: 2023/7/9
 *
 * @author 吃素不吃肉
 */
@Slf4j
@Sharable
@Component
public class RealProxyConnectHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //通道关闭监听
        ctx.channel().closeFuture().addListener((ChannelFuture channelFuture) -> {
            //清理资源
            ProxyResources.clearRealProxyResources(channelFuture.channel());
        });
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        if (ctx.channel().isOpen()) {
            ctx.channel().close();
        }
        super.channelInactive(ctx);
    }
}
